package com.yuren.codecrushlearn.Q5300;

import java.util.HashMap;

/**
 * Created with Intellij IDEA.
 * Description:
 * URL Match
 * @author 10204
 * @date 2025-05-10 22:12
 */
public class Q5302 {
    class TrieNode {
        HashMap<String, TrieNode> children;
        boolean isEnd;

        public TrieNode() {
            children = new HashMap<>();
        }
    }

    private TrieNode root;

    public Q5302() {
        root = new TrieNode();
    }

    public void addWhiteUrl(String whiteListUrl) {
        String[] parts = whiteListUrl.split("/");
        TrieNode cur = root;
        for (String part : parts) {
            if (part.equals("")) {
                continue;
            }
            if (!cur.children.containsKey(part)) {
                cur.children.put(part, new TrieNode());
            }
            cur = cur.children.get(part);
        }
        cur.isEnd = true;
    }

    public boolean permit(String requestUrl) {
        String[] parts = requestUrl.split("/");
        TrieNode cur = root;
        for (String part : parts) {
            if (part.equals("")) {
                continue;
            }
            if (cur.children.containsKey(part)) {
                cur = cur.children.get(part);
                if (cur.isEnd) {
                    return true;
                }
            } else {
                return false;
            }
        }
        return cur.isEnd;
    }
}
